<?php

namespace App\Http\Controllers;

use App\Model\Article;
use Illuminate\Http\Request;
use DB;

class ArticleController extends Controller
{
    public function show($id)
    {
        return view(resolveView(__CLASS__, __FUNCTION__))->with([
            'article' => Article::findOrFail($id)->toArray()
        ]);
    }

    public function showArticle(Request $request, $id)
    {
        $article = Article::findOrFail($id);

        if ($request->ajax()) {
            return response()->json($article);
        }

        return $article;
    }

    public function showList(Request $request)
    {
        $ids = $request->get('ids', []);
        $count = $request->get('count', false);

        if ($count) {
            $articles = DB::table('article')
                ->inRandomOrder()
                ->take($count)
                ->get();
        } elseif (count($ids)) {
            $articles = Article::where('is_active', 1)->whereIn('id', $ids)->get();
        } else {
            $articles = Article::all();
        }

        return response()->json($articles);
    }
}
